package org.example;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Client1 {
    private  static String PORT ;
    private  static String ADDRESS;
    private static String IP;

    public static void main(String[] args) throws IOException {
        proper();
        //调用多线程
        scheduled();
    }
    public static void proper() throws IOException {
        //读取properties
        InputStream inputStream = Client.class.getClassLoader().getResourceAsStream("client.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        //获取端口和地址
        System.out.println("port=" + properties.getProperty("port"));
        System.out.println("address=" + properties.getProperty("address"));
        System.out.println("ip=" + properties.getProperty("ip"));
        PORT = properties.getProperty("port");
        ADDRESS = properties.getProperty("address");
        IP = properties.getProperty("ip");
        inputStream .close();
    }
    public static void scheduled(){
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
        Runnable task = () -> {
            final File folder = new File(ADDRESS);
            readFile(folder);
        };

        // 这里设置的是固定的1秒钟延时
        executor.scheduleAtFixedRate(task, 1000, 1000, TimeUnit.MILLISECONDS);
    }
    //读取文件
    public static void readFile(File folder){
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    readFile(file);
                }
            }
        } else {
            //d:\clouder\client\新建文件夹\23.txt
            System.out.println(folder.getAbsolutePath());
            //23.txt
            System.out.println(folder.getName());
            //执行相关操作
            try {
                Socket socket = new Socket(IP, Integer.parseInt(PORT));
                System.out.println("成功连接到服务器！");

                // 选择要发送的文件
                String filePath = folder.getAbsolutePath(); // 文件路径
                File file = new File(filePath);

                // 获取文件名长度，并将其发送到服务器
                String fileName = file.getName();
                int fileNameLength = fileName.getBytes().length;
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(fileNameLength);

                // 将文件名发送到服务器
                outputStream.write(fileName.getBytes());

                // 读取文件内容并发送到服务器
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int bytesRead;

                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                fileInputStream.close();
                outputStream.close();
                socket.close();
                System.out.println("文件发送成功：" + fileName);

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
